package com.example.demo.service;

import com.example.demo.entity.Like;
import com.example.demo.mapper.LikeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class LikeService {
    @Autowired
    private LikeMapper likeMapper;

    // 点赞
    public void likePost(Long userId, Long postId) {
        if (!isLiked(userId, postId)) { // 检查是否已经点赞
            Like like = new Like();
            like.setUserId(userId);
            like.setPostId(postId);
            likeMapper.insertLike(like);
        }
    }

    // 取消点赞
    public void unlikePost(Long userId, Long postId) {
        if (isLiked(userId, postId)) { // 检查是否已经取消点赞
            likeMapper.deleteLike(userId, postId);
        }
    }

    // 检查用户是否已经点赞
    public boolean isLiked(Long userId, Long postId) {
        return likeMapper.checkLike(userId, postId) > 0;
    }

    // 获取帖子的点赞数
    public int getLikeCount(Long postId) {
        return likeMapper.getLikeCountByPostId(postId);
    }
}
